package com.pan.admin.module.channel.service.impl;

import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.module.channel.dao.LoanChannelFormFieldDao;
import com.pan.admin.module.channel.entity.LoanChannelFormField;
import com.pan.admin.module.channel.service.LoanChannelFormFieldService;
import com.pan.admin.module.loanConfig.dao.LoanFormFieldDao;
import com.pan.admin.module.loanConfig.entity.LoanFormField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 渠道关联动态单组Service业务层处理
 *
 * @author ts
 * @date 2023-09-15
 */
@Service
public class LoanChannelFormFieldServiceImpl implements LoanChannelFormFieldService {
    @Autowired
    private LoanChannelFormFieldDao loanChannelFormFieldDao;

    @Autowired
    private LoanFormFieldDao formFieldDao;

    /**
     * 根据ID获取列表渠道关联动态单组
     *
     * @param id 渠道关联动态单组主键
     * @return 渠道关联动态单组
     */
    @Override
    public LoanChannelFormField getById(Long id) {
        return loanChannelFormFieldDao.getById(id);
    }

    /**
     * 查询渠道关联动态单组列表
     *
     * @param loanChannelFormField 渠道关联动态单组
     * @return 渠道关联动态单组
     */
    @Override
    public List<LoanChannelFormField> list(LoanChannelFormField loanChannelFormField) {
        List<LoanChannelFormField> list = loanChannelFormFieldDao.list(loanChannelFormField);
        return list;
    }

    /**
     * 查询渠道关联列表的树形控件渲染
     */
    @Override
    public List treeList(LoanChannelFormField loanChannelFormField) {
        List<LoanChannelFormField> list = loanChannelFormFieldDao.list(loanChannelFormField);
        List loanChannelParentVos = new ArrayList<>();
        if (!ValidParam.isEmpty(list)) {
            for (LoanChannelFormField field : list) {
                loanChannelParentVos.add(field.getLoanFormFieldId());
            }
        }
        return loanChannelParentVos;
    }

    /**
     * 新增渠道关联动态单组
     *
     * @param loanChannelFormField 渠道关联动态单组
     * @return 结果
     */
    @Override
    public int insertLoanChannelFormField(LoanChannelFormField loanChannelFormField) {
        loanChannelFormField.setCreateTime(DateUtils.getNowDate());
        loanChannelFormField.setCreateBy(SecurityUtils.getUsername());
        return loanChannelFormFieldDao.insertLoanChannelFormField(loanChannelFormField);
    }

    /**
     * 修改渠道关联动态单组
     *
     * @param loanChannelFormField 渠道关联动态单组
     * @return 结果
     */
    @Override
    public int updateLoanChannelFormField(LoanChannelFormField loanChannelFormField) {
        loanChannelFormField.setUpdateTime(DateUtils.getNowDate());
        loanChannelFormField.setUpdateBy(SecurityUtils.getUsername());
        return loanChannelFormFieldDao.updateLoanChannelFormField(loanChannelFormField);
    }

    /**
     * 删除渠道关联表单 树控
     */
    @Override
    public int delete(List<LoanChannelFormField> loanChannelFormFields) {
        int row = 0;
        if (!ValidParam.isEmpty(loanChannelFormFields)) {
            row = loanChannelFormFieldDao.batchDeleteByTree(loanChannelFormFields);
        }
        return row;
    }


    /**
     * 批量删除渠道关联动态单组
     *
     * @param ids 需要删除的渠道关联动态单组主键
     * @return 结果
     */
    @Override
    public int batchDeleteById(Long[] ids) {
        return loanChannelFormFieldDao.batchDeleteById(ids);
    }

    /**
     * 新增渠道关联表单
     *
     * @param loanChannelFormField
     * @return
     */
    @Override
    public int addTree(LoanChannelFormField loanChannelFormField) {
        int i = 0;
        if (ValidParam.isNotNull(loanChannelFormField)) {
            Long loanFormFieldId = loanChannelFormField.getLoanFormFieldId();
            Long loanChannelId = loanChannelFormField.getLoanChannelId();

            if (ValidParam.isNotNull(loanChannelId) && ValidParam.isNotNull(loanFormFieldId)) {
                // 全选
                if (loanFormFieldId == 0) {
                    List<LoanFormField> list = formFieldDao.list(null);
                    List<LoanChannelFormField> loanChannelFormFieldList = new ArrayList<>();

                    if (!ValidParam.isEmpty(list)) {
                        for (LoanFormField loanFormField : list) {
                            LoanChannelFormField field = new LoanChannelFormField();
                            field.setLoanFormFieldId(loanFormField.getId());
                            field.setLoanChannelId(loanChannelId);
                            int count = loanChannelFormFieldDao.count(field);
                            if (count == 0) {
                                field.setCreateTime(DateUtils.getNowDate());
                                loanChannelFormFieldList.add(field);
                            }
                        }
                    }

                    if (!ValidParam.isEmpty(loanChannelFormFieldList)) {
                        i = loanChannelFormFieldDao.batchInsert(loanChannelFormFieldList);
                    }

                }

                //单个新增
                if (loanFormFieldId != 0) {
                    loanChannelFormField.setCreateTime(DateUtils.getNowDate());
                    loanChannelFormField.setCreateBy(SecurityUtils.getUsername());
                    i = loanChannelFormFieldDao.insertLoanChannelFormField(loanChannelFormField);
                }

            }
        }
        return i;
    }

    /**
     * 删除渠道关联动态单组信息
     *
     * @param id 渠道关联动态单组主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id) {
        return loanChannelFormFieldDao.deleteById(id);
    }
}
